import Vue from "vue";
import Vuex from "vuex";
import api from "@/api/index";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userName: "",
    userPhoto: "",
    userRole: "student"
  },
  mutations: {
    setUserName(state, data) {
      state.userName = data;
    },

    setUserRole(state, data) {
      state.userRole = data;
    },

    setUserPhoto(state, data) {
      if (data) {
        state.userPhoto = Vue.prototype.$imageBaseUrl + data;
      } else {
        state.userPhoto = null;
      }
    }
  },
  actions: {
    async getRole({ commit, state }) {
      const res = await api.get("/student/detail", {});
      commit("setUserRole", res.data.role);
      commit("setUserName", res.data.userName);
      commit("setUserPhoto", res.data.photo);
      return res.data;
    }
  },
  modules: {}
});
